package com.shopping.huaju.commodity.service.impl;

import com.shopping.huaju.commodity.dao.AttrAttrgroupRelationDao;
import com.shopping.huaju.commodity.dao.AttrDao;
import com.shopping.huaju.commodity.entity.AttrAttrgroupRelationEntity;
import com.shopping.huaju.commodity.entity.AttrEntity;
import com.shopping.huaju.commodity.entity.vo.AttrGroupWithAttrsVo;
import com.shopping.huaju.commodity.entity.vo.SpuItemAttrGroupVo;
import com.shopping.huaju.commodity.service.AttrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.Query;

import com.shopping.huaju.commodity.dao.AttrgroupDao;
import com.shopping.huaju.commodity.entity.AttrgroupEntity;
import com.shopping.huaju.commodity.service.AttrgroupService;

import javax.annotation.Resource;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {
    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }

    //自定义接口实现 -- 实现根据categoryId 和 id 、 name 三个条件查询返回分页数据结果
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //获取传过来 的查询条件值 (需求:查询条件值与 id 匹配 或者 与 name字段 模糊匹配
        //根据前端指定的属性 key 获取
        String keyValue = (String) params.get("key");
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyValue)) {
            //如果 查询条件值 不为null 并且不是空字符串
            keyValue = keyValue.trim();
            // 即需要添加条件 查询条件值与 id 匹配 或者 与 name字段 模糊匹配
            //wrapper.eq("id",keyValue).or().like("name",keyValue);
            //这样子 该查询条件并非是独立整体的查询条件，如where id = "" or name = "" and xx=""
            String finalKeyValue = keyValue;
            wrapper.and((attrgroupEntityQueryWrapper) -> {
                attrgroupEntityQueryWrapper.eq("id", finalKeyValue).or().like("name", finalKeyValue);
            });//这样子 该查询条件为整体的查询条件，如where (id = "" or name = "") and xx=""
        }
        //如果 查询条件值 无效，则wrapper不指定条件

        //对 categoryId 添加条件
        // 如果 categoryId 传入的是有效值，将categoryId加入到查询条件中
        if (!Objects.equals(categoryId, 0L)) {
            wrapper.eq("category_id", categoryId);
        }
        //如果 categoryId 传入的是无效值，如0，则不将categoryId加入到查询条件中
        IPage<AttrgroupEntity> page = this.page(new Query<AttrgroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    //实现根据属性关联对象的属性id和属性组id 删除/批量删除 关联关系
    @Override
    public void deleteRelation(AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities) {
        attrAttrgroupRelationDao.deleteByEntities(attrAttrgroupRelationEntities);
    }
    //根据业务需求, 根据属性关联对象的属性id和属性组id 将属性关联对象  保存/批量 到数据库中
    @Override
    public void saveRelation(AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities) {
        attrAttrgroupRelationDao.saveByRelationEntities(attrAttrgroupRelationEntities);
    }
    //根据业务需求，根据分类id 返回分类下的属性组和基本属性数据 要求按层级结构AttrGroupWithAttrsVo
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsVo(Long categoryId) {
        //1.根据 categoryId 获取 所有的属性组
        List<AttrgroupEntity> attrgroupEntities =
                this.list(new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));
        //2.使用流式计算 进行封装 List<AttrGroupWithAttrsVo>
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVoList = attrgroupEntities.stream().map(attrgroupEntity -> {
            //根据属性组id获取对应的基本属性
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(attrgroupEntity.getId());
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //将 属性组的字段 拷贝到 Vo对象的对应字段
            BeanUtils.copyProperties(attrgroupEntity, attrGroupWithAttrsVo);
            //封装 对应的 基本属性列表字段
            if(relationAttrs == null){  //如果属性组无对应的基本属性，则返回空的基本属性列表
                attrGroupWithAttrsVo.setAttrs(Collections.emptyList());
            }else {
                attrGroupWithAttrsVo.setAttrs(relationAttrs);
            }
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        //3. 返回
        return attrGroupWithAttrsVoList;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrgroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);

        return vos;
    }


}